﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XRERP.Models;
using XRERP.utils;

namespace XRERP.Controllers
{
    /// <summary>
    /// 其他费用录入
    /// </summary>
    public class OtherFeeEntryController : Controller
    {

        private MySqlDB db = new MySqlDB();

        // GET: OtherFeeEntry
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult getProitems()
        {
            return Json(db.xr_dl_proitems.ToList());
        }

        /// <summary>
        /// 其它费用录入保存
        /// </summary>
        /// <param name="othercose"></param>
        /// <returns></returns>
        public ActionResult SaveOtherFeeEntry(xr_bl_othercost othercose)
        {
            JRow<xr_bl_othercost> json = new JRow<xr_bl_othercost>();

            var probill = db.xr_bl_probillmain.Where(x => x.probillmain_uuid == othercose.probillmain_uuid);

            if (null == probill)
            {
                json.isError = false;
                json.code = 300;
                json.msg = "保存失败，未找到对应的请款单信息";
            }

            try
            {
                othercose.othercost_uuid = Tools.getGuid();
                othercose.validflag = "1";

                db.xr_bl_othercost.Add(othercose);
                if (db.SaveChanges() == 1)
                {
                    json.isError = false;
                    json.code = 200;
                    json.msg = "保存成功!";
                    json.RowData = othercose;
                }
                else
                {
                    json.isError = true;
                    json.code = 300;
                    json.msg = "保存失败，请重试或与管理员联系！";
                }

            }
            catch (Exception e)
            {
                json.code = 505;
                json.isError = true;
                json.msg = e.ToString();
            }

            return Json(json);
        }

        public ActionResult QueryOtherFeeEntry(String probillmain_uuid = "", int page = 1, int rows = 10)
        {
            Euitable<xr_bl_othercost> table = new Euitable<xr_bl_othercost>();
            //设置查询条件
            Func<xr_bl_othercost, bool> where = x => x.validflag == "1";
            Func<xr_bl_othercost, bool> where2 = x => x.probillmain_uuid == probillmain_uuid;
            table.rows = db.xr_bl_othercost
                .Where(where).Where(where2)
                .OrderBy(y => y.gen_time)
                .Skip<xr_bl_othercost>(rows * (page - 1))
                .Take<xr_bl_othercost>(rows)
                .ToList<xr_bl_othercost>();
            /*table.total = db.xr_bl_othercost
                .Where(where).Where(where2)
                .Count();*/
            return Json(table.rows);
        }
    }
}